const log = require("single-line-log").stdout;
const delay = require("./Sleep.jsc")

class Progress {
	list: Array<string>;
	curret: number;
	run: boolean;
	ends: boolean;
	constructor(list: Array<string>) {
		this.list = list;
		this.curret = 0;
		this.run = false;
		this.ends = false;
	}

	start(): void {
		this.run = true;
		this.current();
	}

	async current() {
		if (this.ends == false) {
			log("Downloading " + this.list[this.curret]);
			this.curret = (this.curret == this.list.length - 1) ? 0 : this.curret + 1;
			await delay(100);
			this.current();
			//setTimeout(this.current, 0.1);
		}
	}
	sleep(ms: number) {
		Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
	}

	end() {
		this.ends = true;
		log.clear();
		log("Downloading Done.");
		process.stdout.write("\n");
	}
	
	error() {
		this.ends = true;
        log.clear();
        log("Error!");
		process.stdout.write("\n");
	}
}

module.exports = Progress;